// pages/practiceResults/practiceResults.js
import PracticeResultsApi from '../../api/practiceResults.js'
import CaseStudyTestApi from "../../api/caseStudyTest";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        type:'',
        viewHeight: 0,
        currentTab: 0, //预设当前项的值
        menuBarFlag: false,
        examId:'',
        lunList: [
            {img: 'https://img2.baidu.com/it/u=1630152857,3645253011&amp;fm=26&amp;fmt=auto'},
            {img: 'https://img1.baidu.com/it/u=1545508510,3177925948&amp;fm=26&amp;fmt=auto'},
            {img: 'https://img1.baidu.com/it/u=2180404876,2868417014&amp;fm=26&amp;fmt=auto'},
        ],
        resultData: {
            aggregateScore: 0,//总分
            interpretation: false,//是否资料解读
            bscjScore: {
                sysScore: 0,//系统分
                aggregateScore: 0,//得分
                mzScore: 0,//命中分,
                xlScore: 0,//效率分
                resultList: [],
                resultListDuo: []
            },
            tgjcScore: {
                sysScore: 0,//系统分
                aggregateScore: 0,//得分
                mzScore: 0,//命中分,
                xlScore: 0,//效率分
                jdScore: 0,//解读分
                resultList: [],
                resultListDuo: []
            },
            zdScore: {
                sysScore: 0,//系统分
                aggregateScore: 0,//得分
                resultList: [],
                resultListDuo: [],
                resultListDiscern: [],
                resultListDuoDiscern: []
            }
        },
        tgjcData:{
            keyWord: '',
            treeData: [],
            treeMenuIndex: 0,
            bodyStr: [],
            options: [],
            name: '',
            oneItem: {},
            radio: -1,
            resultFlag: false,
            showPoint:[]
        },
        interpretationModel:false,
        soundModel:false,
        caseId:'',
        optionsKey: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            examId:options.examId,
            type:options.type
        })
        this.queryExamRecords()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // 页面首次渲染完毕时执行
        let height = wx.getSystemInfoSync().windowHeight
        let $navBar = this.selectComponent("#navBar")
        let navBarHeight = $navBar.data.navBarExtendHeight + $navBar.data.navBarHeight
        let viewHeight = height - navBarHeight
        this.setData({
            viewHeight: viewHeight
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    handlerGobackClick(){
        //进行判断，如果是从历史记录进入，执行退回，否则返回到病例列表
        if(this.data.type == 'test'){
            wx.reLaunch({
                url: `/pages/caseStudy/caseStudy`
            })
        }else {
            wx.navigateBack()
        }
    },
    switchTab: function (e) {
        this.setData({
            currentTab: e.currentTarget.dataset.index
        });
        // this.checkCor();
    },
    checkCor: function () {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    scrollChange(e) {
        let query = wx.createSelectorQuery();
        query.select('#header').boundingClientRect(rect => {
            let height = rect.height;
            if (e.detail.scrollTop >= height - 40) {
                this.setData({
                    menuBarFlag: true
                })
            } else {
                this.setData({
                    menuBarFlag: false
                })
            }
        }).exec();
    },
    /**
     * 查询考试结果
     */
    async queryExamRecords() {
        const studentID = wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).id:''
        const param = {
            examId: this.data.examId,
            studentId: studentID,
            selfNum: 1
        }
        const res = await PracticeResultsApi.getExamRecords(param)
        const data = res.data
        const resultData = this.data.resultData
        const caseRunResults = data.caseRunResults[0]
        resultData.aggregateScore = data.score
        resultData.interpretation = caseRunResults.interpretation
        //病史采集
        resultData.bscjScore.sysScore = caseRunResults.askFull
        resultData.bscjScore.score = caseRunResults.askGot
        resultData.bscjScore.mzScore = caseRunResults.askAdd
        resultData.bscjScore.xlScore = caseRunResults.askDeduct
        resultData.bscjScore.resultList = caseRunResults.questions
        resultData.bscjScore.resultListDuo = caseRunResults.questionsDuoDa
        //体格检查
        resultData.tgjcScore.sysScore = caseRunResults.checkFull
        resultData.tgjcScore.score = caseRunResults.checkGot
        resultData.tgjcScore.mzScore = caseRunResults.checkAdd
        resultData.tgjcScore.xlScore = caseRunResults.checkDeduct
        resultData.tgjcScore.resultList = caseRunResults.bodyChecks
        resultData.tgjcScore.resultListDuo = caseRunResults.tigeDuoDa
        //诊断
        resultData.zdScore.sysScore = caseRunResults.diagnozeFull
        resultData.zdScore.score = caseRunResults.diagnozeGot
        resultData.zdScore.resultList = []
        resultData.zdScore.resultListDuo = []
        resultData.zdScore.resultListDiscern = []
        resultData.zdScore.resultListDuoDiscern = []

        caseRunResults.diagnoses.forEach(item => {
            if (item.type == 1) {
                resultData.zdScore.resultList.push(item)
            } else {
                resultData.zdScore.resultListDiscern.push(item)
            }
        })
        caseRunResults.diagnosesDuoDaOne.forEach(item => {
            if (item.type == 1) {
                resultData.zdScore.resultListDuo.push(item)
            } else {
                resultData.zdScore.resultListDuoDiscern.push(item)
            }
        })
        caseRunResults.diagnosesDuoDaTwo.forEach(item => {
            if (item.type == 1) {
                resultData.zdScore.resultListDuo.push(item)
            } else {
                resultData.zdScore.resultListDuoDiscern.push(item)
            }
        })
        this.setData({
            resultData: resultData,
            caseId:caseRunResults.caseId
        })
    },
    /**
     * 体格检查选项取消
     */
    tgjcOptionClose() {
        this.setData({
            interpretationModel: false,
            soundModel:false
        })
    },
    /**
     * 强制解读
     */
    async constraintUnscramble(e) {
        //获取当前对象信息
        const obj = e.currentTarget.dataset['obj']
        //正确答案
        const exactnessIndex = obj.correctOption[0]
        //学员答案
        const index = obj.selectOption == null?-1:obj.selectOption[0]
        //调用接口查询体格检查答案
        const param = {
            caseId: this.data.caseId,
            checkId: obj.id
        }
        const res = await PracticeResultsApi.interpretationDetails(param)
        // const res = await CaseStudyTestApi.getCaseBodyCheckContent(param)
        const objItem = res.data
        const tgjcData = this.data.tgjcData
        tgjcData.name = obj.itemName
        objItem.id = obj.id
        objItem.options.forEach(item => {
            item.key = String.fromCharCode(65 + item.isIndex)
        })
        tgjcData.options = objItem.options
        tgjcData.bodyStr = JSON.parse(objItem.bodyStr)
        tgjcData.radioText = index == -1?'未作答':`${tgjcData.options[index].key}.${tgjcData.options[index].option}`
        tgjcData.radio = index
        tgjcData.rightAnswers = exactnessIndex
        tgjcData.rightAnswersText = String.fromCharCode(65 + exactnessIndex)
        tgjcData.resultFlag = true
        //判断 是 选项还是听诊
        if(obj.type ==2){
            this.setData({
                tgjcData,
                interpretationModel: true
            })
        }else if(obj.type ==3 && obj.checkSound == 3){
            //查询音源文件
            await this.getBodyCheckSound(obj)
        }
    },
    /**
     * 查询听诊
     */
    async getBodyCheckSound(obj){
        const param = {
            caseId: this.data.caseId,
            bodyCheckId: obj.id
        }
        const res = await CaseStudyTestApi.getBodyCheckSound(param)
        const showPoint= res.data.map(item=>{
            return {
                name:item.tagName,
                url:item.url,
                x:'',
                y:'',
                image:''
            }
        })
        const tgjcData = this.data.tgjcData
        tgjcData.showPoint = showPoint
        this.setData({
            tgjcData,
            soundModel:true
        })
    },
})
